<?
    require_once("mvc/action/Action.php");
    require_once("cms/forum/om/TopicReplyPeer.php");

    class SaveAction extends Action {

        private $log;

        public function __construct() {
            $this->log =& LoggerManager::getLogger("SaveAction");
        }

        public function execute(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

            $errors = new ActionMessages();

            $reply = $form->getReply();
            $reply->setModifiedDate(date(_Core::FORMAT_SQLDATETIME));

            $conn = Propel::getConnection();

            $reply->setBody(strip_tags($reply->getBody(), '<p><a><b><i><u><strong><img><br><strike>'));

            try {

                $conn->begin();

                if($reply->getTopicReplyId() > 0) {

                    $reply->setTopicReplyId($reply->getTopicReplyId());

                    // Update the reply
                    TopicReplyPeer::doUpdate($reply, $conn);

                } else {

                    $reply->setInsertDate(date(_Core::FORMAT_SQLDATETIME));
                    $reply->setUsername($request->getUserPrincipal()->getName());
                    $reply->setActive(_Core::AUTO_ACTIVATE);

                    TopicReplyPeer::doInsert($reply, $conn);

                }

                $conn->commit();

            } catch(PropelException $e) {
                $conn->rollback();
                $errors->add(ActionMessage::GLOBAL_MESSAGE,
                    new ActionMessage("error.reply.save", true));
                $this->log->error($e->getMessage());
            }

            // Return any save errors.
            if(!$errors->isEmpty()) {
                $this->saveErrors($request, $errors);
                return $mapping->getInputForward();
            }


            // Reset the replyListForm from & to session.
            $session  = $request->getSession();
            $listForm = $session->getAttribute("cms/replyListForm");
            $listForm->setPage(1);
            $session->setAttribute("cms/replyListForm", $listForm);


            // To the success reply.
            return $mapping->findForward("success");

        }

    }
?>